<?php
include_once ('./main/inc/global.inc.php');
require_once (api_get_path(LIBRARY_PATH).'formvalidator/FormValidator.class.php');

api_block_anonymous_users(); // only users who are logged in can proceed

$this_section='plugins';

$nosession=false;

if(api_get_setting('use_session_mode')=='true' && !$nosession)
{
	if (isset($_GET['inactives'])){
		$display_actives = false;
	}
	else {
		$display_actives = true;
	}
}

Display :: display_header('Plugins Dashboard');

$plugins = Database::get_plugins();
$user_plugins = Database::get_user_plugins($_user['user_id']);

echo '<h3>Upload New Plugins</h3>';
$form = new FormValidator('plugins', 'post', api_get_self()."?{$_SERVER['QUERY_STRING']}", null);
$form->addElement('text', 'name', 'Name');
$form->addRule('name',  get_lang('ThisFieldIsRequired'), 'required');
$form->addElement('textarea', 'desc', 'Description');
$form->addElement('file', 'plugin_file', 'Plugin File');
$form->addRule('plugin_file',  get_lang('ThisFieldIsRequired'), 'required');
$form->addRule('plugin_file', 'Only JavaScript file is allowed.', 'filetype', array('js'));
$form->add_progress_bar();
$form->addElement('submit', null, "Upload plugin");
if ($form->validate()) {
    $values = $form->exportValues();
    if ($_FILES['plugin_file']['size']) {
        $name_array = explode('.', $_FILES['plugin_file']['name']);
        $name_array[count($name_array)-2] .= time();
        $filename = implode('.', $name_array);
        $source = $_FILES['plugin_file']['tmp_name'];
        $target_path = '/home/student/public_html/dokeos/plugins/'.$filename;
        if (move_uploaded_file($source, $target_path)) {
            $path = '/dokeos/plugins/'.$filename;
            Database::add_plugin($values['name'], $values['desc'], $path);
            Display::display_confirmation_message('<p>Plugin file has been uploaded.</p>', false);
        } else {
            Display::display_error_message('<p>Eerror when uploading. Please try again later.</p>', false);
        }
    }
} else {
  $form->display();
}
echo '<h3>Existing plugins</h3>';
if (count($plugins) > 0) {
    echo '<table>';
    echo '<thead><th>id</th><th>name</th><th>Description</th><th>Action</th></thead>';
    foreach ($plugins as $p) {
        echo '<tr>';
        echo '<td class="plugin_id">'.$p['id'].'</td>';
        echo '<td>'.$p['name'].'</td>';
        echo '<td>'.$p['description'].'</td>';
        if (in_array($p['id'], $user_plugins)) {
            echo '<td><input class="install_plugin uninstall" type="button" value="Uninstall"/>';
        } else {
            echo '<td><input class="install_plugin" type="button" value="Install"/>';
        }
        echo '<a class="download_plugin" href="'.$p['path'].'" download="'.$p['name'].'.js">Download</a></td>';
        echo '</tr>';
    }
    echo '</table>';
} else {
    echo '<p>There is no plugin in the system</p>';
}
?>
<script>
$(document).ready(function() {
    $('.install_plugin').click(function() {
        $ibt = $(this);
        if ($ibt.hasClass('uninstall')) {
            $.ajax({
                url: '/dokeos/ajax.php',
                data: {
                    request: 'uninstall_plugin',
                    user_id: '<?php echo $_user['user_id'];?>',
                    plugin_id: $(this).closest('tr').find('.plugin_id').html()
                },
                dataType: 'json',
                success: function(msg) {
                    $ibt.attr('value', 'Install');
                    $ibt.removeClass('uninstall');
location.reload(false);
                }
            });
        } else {
            $.ajax({
                url: '/dokeos/ajax.php',
                data: {
                    request: 'install_plugin',
                    user_id: '<?php echo $_user['user_id'];?>',
                    plugin_id: $(this).closest('tr').find('.plugin_id').html()
                },
                dataType: 'json',
                success: function(msg) {
                    $ibt.attr('value', 'Uninstall');
                    $ibt.addClass('uninstall');
location.reload(false);
                }
            });
        }
    });
});
</script>

<a href="<?php echo api_get_path(WEB_PATH); ?>">&lt;&lt; <?php echo get_lang('Back'); ?></a>

<?php
Display :: display_footer();
?>
